<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="小区名称" prop="premiseName">
        <el-input
          v-model="queryParams.premiseName"
          placeholder="请输入小区名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
        <el-form-item label="租客姓名" prop="tenantName">
          <el-input
            v-model="queryParams.tenantName"
            placeholder="请输入租客姓名"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="收款单号" prop="receiptOrderNo">
        <el-input
          v-model="queryParams.receiptOrderNo"
          placeholder="请输入收款单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
        </el-form-item><br>
        <el-form-item label="收款状态">
          <el-select v-model="queryParams.skStatus" clearable>
            <el-option
              v-for="item in dict.type.affairs_receipt_status"
              :value="item.value"
              :label="item.label">
            </el-option>
          </el-select>
        </el-form-item>
      <el-form-item label="账单类型">
        <el-select v-model="queryParams.billType" clearable>
          <el-option
            v-for="item in dict.type.affairs_check_type"
            :value="item.value"
            :label="item.label">
          </el-option>
        </el-select>
      </el-form-item>
        <el-form-item label="租房合同编号" prop="contractNo">
          <el-input
            v-model="queryParams.contractNo"
            placeholder="请输入租房合同编号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item><br>
      <el-form-item label="应收日期">
        <el-date-picker
          v-model="queryParams.beginCollectionDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期" clearable>
        </el-date-picker>
        <el-date-picker
          v-model="queryParams.endCollectionDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期" clearable>
        </el-date-picker>
      </el-form-item><br>
      <el-form-item label="实收日期">
        <el-date-picker
          v-model="queryParams.beginRealCollectionDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期" clearable>
        </el-date-picker>
        <el-date-picker
          v-model="queryParams.endRealCollectionDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期" clearable>
        </el-date-picker>
      </el-form-item>





      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['affairs:cwReceiptPlan:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <template>
      <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
        <el-tab-pane label="租房收款计划" name="first">
          <span>应收金额总计：{{this.amountReceivable}}</span>&nbsp;
          <span>实收金额总计：{{this.amountCollected}}</span>
          <el-table v-loading="loading" :data="cwReceiptPlanList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="租房合同编号" align="center" prop="contractNo" />
            <el-table-column label="收款单号" align="center" prop="receiptOrderNo" />
            <el-table-column label="承租房间、" align="center" prop="roomName" />
            <el-table-column label="租客姓名、" align="center" prop="tenantName" />
            <el-table-column label="小区名称" align="center" prop="premiseName" />
            <el-table-column label="支付金额" align="center" prop="paymentAmount" />
            <el-table-column label="账单" align="center" prop="receiptNumber">
              <template slot-scope="scope">
                <span>第{{scope.row.receiptNumber}}期</span>
              </template>
            </el-table-column>
            <el-table-column label="收款状态" align="center" prop="skStatus">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.affairs_receipt_status" :value="scope.row.skStatus"/>
              </template>
            </el-table-column>
            <el-table-column label="账单类型" align="center" prop="billType">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.affairs_check_type" :value="scope.row.billType"/>
              </template>
            </el-table-column>
            <el-table-column label="应收日期" align="center" prop="collectionDate" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.collectionDate, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="实收日期" align="center" prop="realCollectionDate" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.realCollectionDate, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="支付方式" align="center" prop="payMethod">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.affairs_pay_way" :value="scope.row.payMethod"/>
              </template>
            </el-table-column>
            <el-table-column label="操作人" align="center" prop="operationUser"/>
          </el-table>
          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-tab-pane>
        <el-tab-pane label="租务收款计划" name="second">
          <span>应收金额总计：{{this.amountReceivable}}</span>&nbsp;
          <span>实收金额总计：{{this.amountCollected}}</span>
          <el-table v-loading="loading" :data="cwReceiptPlanList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="租房合同编号" align="center" prop="contractNo" />
            <el-table-column label="收款单号" align="center" prop="receiptOrderNo" />
            <el-table-column label="承租房间、" align="center" prop="roomName" />
            <el-table-column label="租客姓名、" align="center" prop="tenantName" />
            <el-table-column label="小区名称" align="center" prop="premiseName" />
            <el-table-column label="支付金额" align="center" prop="paymentAmount" />
            <el-table-column label="账单" align="center" prop="receiptNumber">
              <template slot-scope="scope">
                <span>第{{scope.row.receiptNumber}}期</span>
              </template>
            </el-table-column>
            <el-table-column label="收款状态" align="center" prop="skStatus">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.affairs_receipt_status" :value="scope.row.skStatus"/>
              </template>
            </el-table-column>
            <el-table-column label="账单类型" align="center" prop="billType">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.affairs_check_type" :value="scope.row.billType"/>
              </template>
            </el-table-column>
            <el-table-column label="应收日期" align="center" prop="collectionDate" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.collectionDate, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="实收日期" align="center" prop="realCollectionDate" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.realCollectionDate, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="支付方式" align="center" prop="payMethod">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.affairs_pay_way" :value="scope.row.payMethod"/>
              </template>
            </el-table-column>
            <el-table-column label="操作人" align="center" prop="operationUser"/>
          </el-table>
          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-tab-pane>
      </el-tabs>
    </template>
  </div>
</template>

<script>
import { listCwReceiptPlan, getCwReceiptPlan, delCwReceiptPlan, addCwReceiptPlan, updateCwReceiptPlan } from "@/api/affairs/cwReceiptPlan";

export default {
  name: "CwReceiptPlan",
  dicts: ['affairs_receipt_status','affairs_check_type','affairs_pay_way'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 收款计划表格数据
      cwReceiptPlanList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //  选项卡样式
      activeName: 'first',
      amountReceivable: null,
      amountCollected: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        //默认值为 1
        operationTypeDefault: 1,
        beginCollectionDate: null,
        endCollectionDate: null,
        beginRealCollectionDate: null,
        endRealCollectionDate: null,
        receiptOrderNo: null,
        businessId: null,
        contractId: null,
        contractNo: null,
        contractType: null,
        tTerminationId: null,
        premiseId: null,
        premiseName: null,
        roomId: null,
        roomCode: null,
        roomName: null,
        ownerId: null,
        ownerName: null,
        tenantId: null,
        tenantName: null,
        receiptType: null,
        skStatus: null,
        billType: null,
        itemType: null,
        otherItemName: null,
        paymentAmount: null,
        realPaymentAmount: null,
        withReceiptPlanId: null,
        paymentDaysBegin: null,
        paymentDaysEnd: null,
        collectionDate: null,
        realCollectionDate: null,
        receiptNumber: null,
        payMethod: null,
        otherPayMethod: null,
        payFlowNo: null,
        rentalPrice: null,
        tax: null,
        operationUser: null,
        reserve1: null,
        reserve2: null,
        reserve3: null,
        reserve4: null,
        reserve5: null,
        delFlg: null,
        createUserid: null,
        updateUserid: null,
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询收款计划列表 */
    getList() {
      this.loading = true;
      listCwReceiptPlan(this.queryParams).then(response => {
        this.cwReceiptPlanList = response.list.rows;
        this.total = response.list.total;
        this.amountReceivable=response.amountReceivable;
        this.amountCollected=response.amountCollected;
        this.loading = false;
      });
    },
    //获取选项卡数据
    handleClick(tab, event) {
      //每次查询前都前往第一页
      this.queryParams.pageNum=1;
      //每次跳转前先将计算总值清零
      this.amountReceivable=0;
      this.amountCollected=0;
      if (tab.name=="first"){
        //查询租房收款计划
        this.queryParams.operationTypeDefault=1;
      }else {
        //查询租务收款计划
        this.queryParams.operationTypeDefault=2;
      }
      this.getList();
      console.log(tab, event);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        receiptOrderNo: null,
        businessId: null,
        contractId: null,
        contractNo: null,
        contractType: null,
        tTerminationId: null,
        premiseId: null,
        premiseName: null,
        roomId: null,
        roomCode: null,
        roomName: null,
        ownerId: null,
        ownerName: null,
        tenantId: null,
        tenantName: null,
        receiptType: null,
        skStatus: 0,
        billType: null,
        itemType: null,
        otherItemName: null,
        paymentAmount: null,
        realPaymentAmount: null,
        withReceiptPlanId: null,
        paymentDaysBegin: null,
        paymentDaysEnd: null,
        collectionDate: null,
        realCollectionDate: null,
        receiptNumber: null,
        payMethod: null,
        otherPayMethod: null,
        payFlowNo: null,
        rentalPrice: null,
        tax: null,
        operationUser: null,
        remark: null,
        reserve1: null,
        reserve2: null,
        reserve3: null,
        reserve4: null,
        reserve5: null,
        delFlg: null,
        createUserid: null,
        createTime: null,
        updateUserid: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加收款计划";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCwReceiptPlan(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改收款计划";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCwReceiptPlan(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCwReceiptPlan(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除收款计划编号为"' + ids + '"的数据项？').then(function() {
        return delCwReceiptPlan(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('affairs/cwReceiptPlan/export', {
        ...this.queryParams
      }, `cwReceiptPlan_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
